package com.bishe.demo.modules.tourism.service.impl;

/**
 * @Description RouteOrderServiceImpl
 * @Author Lubb
 * @Date 2021/4/9 10:32
 */

import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.tourism.dao.RouteOrderDao;
import com.bishe.demo.modules.tourism.entity.RouteOrder;
import com.bishe.demo.modules.tourism.service.RouteOrderService;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class RouteOrderServiceImpl implements RouteOrderService {
    @Autowired
    private RouteOrderDao routeOrderDao;

    @Override
    @Transactional
    public ResultEntity<RouteOrder> addRouteOrder(RouteOrder routeOrder) {
        routeOrderDao.addRouteOrder(routeOrder);
        return new ResultEntity<RouteOrder>(ResultEntity.ResultStatus.SUCCESS.code,
                "add new order success", routeOrder);
    }

    @Override
    public ResultEntity<RouteOrder> deleteRouteOrderById(Integer id) {
        routeOrderDao.deleteRouteOrderById(id);
        return new ResultEntity<RouteOrder>(ResultEntity.ResultStatus.SUCCESS.code,
                "delete order success");
    }

    @Override
    @Transactional
    public ResultEntity<RouteOrder> updateRouteOrderComment(RouteOrder routeOrder) {
        routeOrderDao.updateRouteOrderComment(routeOrder);
        return new ResultEntity<RouteOrder>(ResultEntity.ResultStatus.SUCCESS.code,
                "update order success",routeOrder);
    }

    @Override
    public List<RouteOrder> getRouteOrderByUserId(Integer userId) {
        List<RouteOrder> routeOrders = routeOrderDao.getRouteOrderByUserId(userId);
        for (RouteOrder routeOrder : routeOrders) {
            if (routeOrder != null) {
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                routeOrder.setStatus(LocalDate.now().isAfter(LocalDate.parse(routeOrder.getOrderingTime(), fmt)) ? "1" : "0");
            }
        }
        return routeOrders;
    }
}
